package com.cloudcc.mobile.db;

import android.content.Context;

import com.cloudcc.mobile.entity.PushSaveMessage;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;

import java.util.List;
import java.util.concurrent.ScheduledExecutorService;

/**
 * Created by dev on 2017/7/31.
 */

public class PushSaveMessageDB {
    private Context context;
    private DbUtils db;

    public PushSaveMessageDB(Context context, String dbName) {
        db = DbUtils.create(context, dbName);
        this.context = context;
    }

    /**
     * 保存数据库
     *
     * @param cls
     * @throws DbException
     */
    public void savePushSaveMessage(PushSaveMessage entity) throws DbException {
        db.save(entity);
    }

    public void saveListPushSaveMessage(List<PushSaveMessage> list) throws DbException {
        db.saveAll(list);
    }

    /**
     * 查询所有消息
     *
     * @param <T>
     */
    public <T> List<PushSaveMessage> findAllPushSaveMessage(Class<T> cls) throws DbException {
        @SuppressWarnings("unchecked")
        List<PushSaveMessage> list = (List<PushSaveMessage>) db.findAll(cls);
        return list;
    }

    /**
     * 删除某个id的消息
     *
     * @param <T>
     * @throws DbException
     */
    public <T> void deleteById(Class<T> cls, int id) throws DbException {
        db.deleteById(cls, id);
    }

    /**
     * 删除所有消息
     *
     * @param <T>
     * @throws DbException
     */
    public <T> void deleteAll(Class<T> cls) throws DbException {
        db.deleteAll(cls);
    }

    /**
     * 获取系统新消息数量
     *
     * @param <T>
     * @throws DbException
     */

    public long getNewPushSaveMessageNum() throws DbException {
        if (isExit()) {
            long num = db.count(Selector.from(PushSaveMessage.class).where("isNew",
                    "=", "1"));
            return num;
        } else {
            return 0;
        }
    }
    /**
     * 清空消息数量
     *
     * @throws DbException
     */

    public void clearPushSaveMessageNum() throws DbException {
        List<PushSaveMessage> list = db.findAll(Selector.from(PushSaveMessage.class));
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setIsNew("0");
        }
        db.saveOrUpdateAll(list);
    }

    /**
     * 判别是否存在
     *
     * @return
     * @throws DbException
     */
    public boolean isExit() throws DbException {
        boolean istable = db.tableIsExist(PushSaveMessage.class);
        if (istable) {
            long a = db.count(PushSaveMessage.class);
            if (a > 0) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public PushSaveMessage getLastPushSaveMessage() throws DbException {
        if (isExit()) {
            List<PushSaveMessage> list = db.findAll(PushSaveMessage.class);
            return list.get(list.size() - 1);
        } else {
            return null;
        }

    }

    /**
     * 修改
     *
     * @param cls
     * @throws DbException
     */
    public void updateIsNew(PushSaveMessage entity,int id) throws DbException {
        List<PushSaveMessage> list = db.findAll(PushSaveMessage.class);
        for (int i = 0; i < list.size(); i++) {
                list.get(i).setIsNew("0");
                db.update(list.get(i),WhereBuilder.b("id","=",id),"isNew");

        }

    }
}
